package com.hyh.music.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyh.music.Expections.RepeatException;
import com.hyh.music.Result.PageResult;
import com.hyh.music.domain.domainDTO.MessagePageDTO;
import com.hyh.music.domain.domains.ListSong;
import com.hyh.music.domain.domains.Singer;
import com.hyh.music.domain.domains.Song;
import com.hyh.music.mapper.ListSongMapper;
import com.hyh.music.mapper.SingerMapper;
import com.hyh.music.mapper.SongMapper;
import com.hyh.music.service.ListSongService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hyh
 * @version 1.0
 */
@SuppressWarnings({"ALL"})
@Slf4j
@Service
public class ListSongServiceImpl implements ListSongService {


    @Autowired
    ListSongMapper listSongMapper;

    @Autowired
    SongMapper songMapper;

    @Autowired
    SingerMapper singerMapper;
    /**
     * 分页查询
     * @param messagePageDTO
     * @return
     */
    @Override
    public PageResult Page(MessagePageDTO messagePageDTO) {
        PageHelper.startPage(messagePageDTO.getPage(), messagePageDTO.getPageSize());
        Page<ListSong> page = listSongMapper.Page(messagePageDTO);
        List<ListSong> result = page.getResult();
        long total = page.getTotal();
        PageResult result1 = new PageResult();
        result1.setRecodes(result);
        result1.setTotal(total);
        return result1;
    }

    @Override
    public void delete(List<Integer> ids){
        listSongMapper.delete(ids);
    }

    /**
     * 添加歌曲到歌单
     * @param songIds
     * @param id
     */
    @Override
    public void add(List<Integer> songIds, Integer id) {

        for (Integer songId : songIds) {
            Song song = songMapper.selectById(songId);

            //查询是否重复
            List<ListSong> listSongs = songMapper.selectIfRepeat(songId , id);

            if (listSongs.size() != 0 && listSongs != null){
                String repectName =  listSongs.get(0).getSongName();
                throw new RepeatException(repectName + "重复了");
            }

            Integer singerId = song.getSingerId();
            Singer singer = singerMapper.selectById(singerId);
            String name = singer.getName() + "-" + song.getName();

            ListSong listSong = new ListSong();
            listSong.setSongListId(id);
            listSong.setSongId(song.getId());
            listSong.setSongName(name);

            listSongMapper.add(listSong);

        }
    }
}
